﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Configuration;

namespace Crm.Web
{
    public class RaporProcess
    {
        private const char satirAyrac = '¤';
        private const char alanAyrac = '¥';
        private const char veriAyrac = '§';
        string DefaultValue(string type)
        {
            switch (type)
            {
                case "System.String":
                    return string.Empty;
                case "System.Byte":
                case "System.Single":
                case "System.Double":
                case "System.Decimal":
                case "System.Int64":
                case "System.Int16":
                case "System.Int32":
                    return "0";
                case "System.DateTime":
                    return "01/01/1899";
                case "System.Boolean":
                    return "false";
                default:
                    return "";
            }
        }
        string ConnString = WebConfigurationManager.ConnectionStrings["mikrodb"].ConnectionString;

        public string GetData(string sql)
        {
            StringBuilder sb = new StringBuilder();
            using (SqlConnection cn = new SqlConnection(ConnString))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(sql, cn))
                {
                    SqlDataReader rd = cmd.ExecuteReader();
                    for (int i = 0; i < rd.FieldCount; i++)
                    {
                        sb.AppendFormat("{0}{1}{2}{3}", rd.GetName(i), alanAyrac, rd.GetFieldType(i).ToString(), satirAyrac);
                    }
                    sb.Remove(sb.Length - 1, 1);
                    sb.Append(veriAyrac);

                    while (rd.Read())
                    {
                        for (int i = 0; i < rd.FieldCount; i++)
                        {
                            if (rd[i] != DBNull.Value)
                            {
                                if (rd.GetFieldType(i) == typeof(DateTime))
                                {
                                    sb.Append(rd.GetDateTime(i).ToStr());
                                }
                                else
                                {
                                    sb.Append(rd[i].ToString().Replace("\r\n", ""));
                                }
                                sb.Append(alanAyrac);
                            }
                            else
                            {
                                sb.Append(DefaultValue(rd.GetFieldType(i).ToString()) + alanAyrac);
                            }
                        }
                        sb.Append(satirAyrac);
                    }
                    sb.Remove(sb.Length - 1, 1);
                }
                cn.Close();
            }
            return sb.ToString();
        }
    }
}